package zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.politicalstatus.PoliticalStatusInfo;
import zzs.generalframework.common.core.parameter.commonparameter.politicalstatus.PoliticalStatusPO;
import zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.dao.IPoliticalStatusDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.politicalstatus.dao.impl.politicalStatusDaoImpl")
public class PoliticalStatusDaoImpl implements IPoliticalStatusDao
{
	private Logger logger = LoggerFactory.getLogger(PoliticalStatusDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(PoliticalStatusPO politicalStatusPO)
	{
		logger.info("====插入政治面貌数据开始====");
		logger.debug("====插入政治面貌数据,入参:PoliticalStatusPO=[{}]====", politicalStatusPO);
		entityManager.persist(politicalStatusPO);
		logger.debug("====插入政治面貌数据,出参:boolean=[{}]====", true);
		logger.info("====插入政治面貌数据结束====");
		return true;
	}

	@Override
	public boolean update(PoliticalStatusPO politicalStatusPO)
	{
		logger.info("====更新政治面貌数据开始====");
		logger.debug("====更新政治面貌数据,入参:PoliticalStatusPO=[{}]====", politicalStatusPO);
		entityManager.merge(politicalStatusPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新政治面貌数据结束====");
		return true;
	}

	@Override
	public List<PoliticalStatusPO> select(PoliticalStatusInfo politicalStatusInfo)
	{
		logger.info("====查询政治面貌数据开始====");
		logger.debug("====查询政治面貌数据,入参:PoliticalStatusInfo=[{}]====", politicalStatusInfo);
		List<PoliticalStatusPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from PoliticalStatusPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(politicalStatusInfo.getPoliticalStatusID()))
		{
			sqlBuffer.append(" and a.politicalStatusID=?");
			parameter.putParameterObject(politicalStatusInfo.getPoliticalStatusID());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusInfo.getPoliticalStatusCode()))
		{
			sqlBuffer.append(" and a.politicalStatusCode=?");
			parameter.putParameterObject(politicalStatusInfo.getPoliticalStatusCode());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusInfo.getPoliticalStatusName()))
		{
			sqlBuffer.append(" and a.politicalStatusName=?");
			parameter.putParameterObject(politicalStatusInfo.getPoliticalStatusName());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(politicalStatusInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(politicalStatusInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询政治面貌数据,出参:List<PoliticalStatusPO>=[{}]====", returnList);
		logger.info("====查询政治面貌数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(PoliticalStatusPO politicalStatusPO)
	{
		logger.info("====删除政治面貌数据开始====");
		logger.debug("====删除政治面貌数据,入参:PoliticalStatusPO=[{}]====", politicalStatusPO);
		entityManager.remove(politicalStatusPO);
		logger.debug("====删除政治面貌数据,出参:boolean=[{}]====", true);
		logger.info("====删除政治面貌数据结束====");
		return true;
	}

	@Override
	public Long count(PoliticalStatusPO politicalStatusPO)
	{
		logger.info("====统计政治面貌数据开始====");
		logger.debug("====统计政治面貌数据,入参:PoliticalStatusPO=[{}]====", politicalStatusPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from PoliticalStatusPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(politicalStatusPO.getPoliticalStatusID()))
		{
			sqlBuffer.append(" and a.politicalStatusID=?");
			parameter.putParameterObject(politicalStatusPO.getPoliticalStatusID());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusPO.getPoliticalStatusCode()))
		{
			sqlBuffer.append(" and a.politicalStatusCode=?");
			parameter.putParameterObject(politicalStatusPO.getPoliticalStatusCode());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusPO.getPoliticalStatusName()))
		{
			sqlBuffer.append(" and a.politicalStatusName=?");
			parameter.putParameterObject(politicalStatusPO.getPoliticalStatusName());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(politicalStatusPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(politicalStatusPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(politicalStatusPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计政治面貌数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计政治面貌数据结束====");
		return returnLong;
	}

}
